package com.iot.inclinometer.util;

public class MeanMethod {

	/**
	 * 求算数平均数
	 * @param x1Array
	 * @param y1Array
	 * @param x2Array
	 * @param y2Array
	 * @return
	 */
	public static double[] getArithmeticMean(double[] xArray,double[] yArray,double[] zArray,double[] tArray){
		int n=xArray.length;
		double xSum=0;
		double ySum=0;
		double zSum=0;
		double tSum=0;
		for(int i=0;i<n;i++){
         	xSum+=xArray[i];
         	ySum+=yArray[i];
         	zSum+=zArray[i];
         	tSum+=tArray[i];
        }
		double[] arithmeticMean=new double[4];
        arithmeticMean[0] = (double)(xSum/n);  //求x1[0:n-1]的平均数
        arithmeticMean[1] = (double)(ySum/n);
        arithmeticMean[2] = (double)(zSum/n);
        arithmeticMean[3] = (double)(tSum/n);//返回的温度为处理后温度
		return arithmeticMean;
	}
	/**
	 * 求平方平均数
	 * @param x1Array
	 * @param y1Array
	 * @param x2Array
	 * @param y2Array
	 * @return
	 */
	public static double[] getQuadraticMean(double[] xArray,double[] yArray,double[] zArray,double[] tArray){
		int n=xArray.length;
		double xSquareSum=0;
		double ySquareSum=0;
		double zSquareSum=0;
		double tSquareSum=0;
		for(int i=0;i<n;i++){
         	xSquareSum+=xArray[i]*xArray[i];
         	ySquareSum+=yArray[i]*yArray[i];
         	zSquareSum+=zArray[i]*zArray[i];
         	tSquareSum+=tArray[i]*tArray[i];
        }
		double[] quadraticMean=new double[4];
		quadraticMean[0] = Math.sqrt(xSquareSum/n);  
		quadraticMean[1] = Math.sqrt(ySquareSum/n);
		quadraticMean[2] = Math.sqrt(zSquareSum/n);
		quadraticMean[3] = Math.sqrt(tSquareSum/n);
		return quadraticMean;
	}
	
	/**
	 * 求几何平均数
	 * @param x1Array
	 * @param y1Array
	 * @param x2Array
	 * @param y2Array
	 * @return
	 */
	public static double[] getGeometricMean(double[] xArray,double[] yArray,double[] zArray,double[] tArray){
		int n=xArray.length;
		double xProduct=1;
		double yProduct=1;
		double zProduct=1;
		double tProduct=1;
		for(int i=0;i<n;i++){
			xProduct*=Math.abs(xArray[i]);
			yProduct*=Math.abs(yArray[i]);
			zProduct*=Math.abs(zArray[i]);
			tProduct*=Math.abs(tArray[i]);
        }
		double[] geometricMean=new double[4];
		geometricMean[0] = (double)(Math.pow(xProduct, n));  
		geometricMean[1] = (double)(Math.pow(yProduct, n));
		geometricMean[2] = (double)(Math.pow(zProduct, n));
		geometricMean[3] = (double)(Math.pow(tProduct, n));
		return geometricMean;
	}
	/**
	 * 求调和平均数
	 * @param x1Array
	 * @param y1Array
	 * @param x2Array
	 * @param y2Array
	 * @return
	 */
	public static double[] getHarmonicMean(double[] xArray,double[] yArray,double[] zArray,double[] tArray){
		int n=xArray.length;
		double xSum=0;
		double ySum=0;
		double zSum=0;
		double tSum=0;
		for(int i=0;i<n;i++){
         	xSum+=1.0/xArray[i];
         	ySum+=1.0/yArray[i];
         	zSum+=1.0/zArray[i];
         	tSum+=1.0/tArray[i];
        }
		double[] harmonicMean=new double[4];
		harmonicMean[0] = (double)(1.0/(xSum/n));
		harmonicMean[1] = (double)(1.0/(ySum/n));
		harmonicMean[2] = (double)(1.0/(zSum/n));
        harmonicMean[3] = (double)(1.0/(tSum/n));
		return harmonicMean;
	}
}
